package com.solcoo.customer.ui.follow;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import com.android.common.util.DialogUtil;
import com.android.common.util.ToastUtil;
import com.solcoo.customer.ApplicationExt;
import com.solcoo.customer.Constant;
import com.solcoo.customer.base.task.TaskHelper;
import com.solcoo.customer.base.task.TaskHelper.Callback;
import com.solcoo.customer.base.task.TaskHelper.DefaultCallback;
import com.solcoo.customer.model.BaseDO;
import com.solcoo.customer.ui.ToolBarActivity;
import com.solcoo.customer.util.CheckUtil;
import com.solcoo.customer.util.CheckUtil.CheckResult;
import com.solcoo.customer.util.ViewUtil;

public class FollowActivity extends ToolBarActivity {

	private Dialog mDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mDialog = DialogUtil.createDialog(this, successOnClickListener, null,
				null, "关注成功", null);
		ViewUtil.startCapture(this, 0);
	}

	@Override
	protected int getCurId() {
		return 0;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		// 处理扫描结果（在界面上显示）
		if (resultCode == RESULT_OK) {
			Bundle bundle = data.getExtras();
			String scanResult = bundle.getString("result");
			CheckResult result = CheckUtil.checkQrcode(scanResult);
			if (result.isSuccess()) {
				TaskHelper.oper(this, Constant.URL.getFollow(
						ApplicationExt.getUser().yonghum, scanResult),
						callback, BaseDO.class);
				return;
			} else {
				ToastUtil.showToast(this, "扫描内容为：" + scanResult);
				finish();
			}
		}
		finish();
	}

	private Callback<BaseDO> callback = new DefaultCallback<BaseDO>(this) {

		@Override
		public void onSuccess(BaseDO result) {
			mDialog.show();
		}

		@Override
		public void onFail(BaseDO result) {
			super.onFail(result);
			finish();
		}
	};

	private DialogInterface.OnClickListener successOnClickListener = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			finish();
		}
	};

}
